package com.ruoyi.whole.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.evaluate.infoprojectevaluate.domain.InfoProjectEvaluate;
import com.ruoyi.evaluate.infoprojectevaluate.mapper.InfoProjectEvaluateMapper;
import com.ruoyi.evaluate.infoprojectevaluateindex.domain.InfoProjectEvaluateIndex;
import com.ruoyi.evaluate.infoprojectevaluateindex.mapper.InfoProjectEvaluateIndexMapper;
import com.ruoyi.monitor.infoprojectmonitor.domain.InfoProjectMonitor;
import com.ruoyi.monitor.infoprojectmonitor.mapper.InfoProjectMonitorMapper;
import com.ruoyi.monitor.infoprojectmonitorindex.domain.InfoProjectMonitorIndex;
import com.ruoyi.project.infoprojectcapitalannex.domain.InfoProjectCapitalAnnex;
import com.ruoyi.project.infoprojecttarget.domain.InfoProjectTarget;
import com.ruoyi.project.infoprojecttarget.service.impl.InfoProjectTargetServiceImpl;
import com.ruoyi.project.infoprojecttargetscore.domain.InfoProjectTargetScore;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.whole.domain.*;
import com.ruoyi.whole.mapper.InfoWholeEvaluateMapper;
import com.ruoyi.whole.mapper.InfoWholeIndexMapper;
import com.ruoyi.whole.mapper.InfoWholePlanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.whole.mapper.InfoWholeTargetMapper;
import com.ruoyi.whole.service.IInfoWholeTargetService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 目标管理-部门整体信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-10
 */
@Service
public class InfoWholeTargetServiceImpl implements IInfoWholeTargetService {
    private static final Logger log = LoggerFactory.getLogger(InfoWholeTargetServiceImpl.class);
    @Autowired
    private InfoWholeTargetMapper infoWholeTargetMapper;

    @Autowired
    private InfoWholeIndexMapper infoWholeIndexMapper;

    @Autowired
    private InfoWholePlanMapper infoWholePlanMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private InfoProjectEvaluateMapper infoProjectEvaluateMapper;
    @Autowired
    private InfoProjectEvaluateIndexMapper infoProjectEvaluateIndexMapper;
    @Autowired
    private InfoProjectMonitorMapper infoProjectMonitorMapper;
    @Autowired
    private InfoWholeEvaluateMapper infoWholeEvaluateMapper;

    /**
     * 查询目标管理-部门整体信息
     *
     * @param id 目标管理-部门整体信息主键
     * @return 目标管理-部门整体信息
     */
    @Override
    public InfoWholeTarget selectInfoWholeTargetById(Long id) {
        return infoWholeTargetMapper.selectInfoWholeTargetById(id);
    }

    /**
     * 查询目标管理-部门整体信息列表
     *
     * @param infoWholeTarget 目标管理-部门整体信息
     * @return 目标管理-部门整体信息
     */
    @Override
    public List<InfoWholeTarget> selectInfoWholeTargetList(InfoWholeTarget infoWholeTarget) {
        return infoWholeTargetMapper.selectInfoWholeTargetList(infoWholeTarget);
    }

    /**
     * 新增目标管理-部门整体信息
     *
     * @param infoWholeTarget 目标管理-部门整体信息
     * @return 结果
     */
    @Override
    public int insertInfoWholeTarget(InfoWholeTarget infoWholeTarget) {
        infoWholeTarget.setCreateTime(DateUtils.getNowDate());
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        infoWholeTarget.setDepartmentalYear(year);
        SysDept sysDept = sysDeptMapper.selectDeptListsss("审计处");
        infoWholeTarget.setDeptId(sysDept.getDeptId());
        return infoWholeTargetMapper.insertInfoWholeTarget(infoWholeTarget);
    }

    /**
     * 修改目标管理-部门整体信息
     *
     * @param infoWholeTarget 目标管理-部门整体信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateInfoWholeTarget(InfoWholeTarget infoWholeTarget) {
        infoWholeTarget.setUpdateTime(DateUtils.getNowDate());
        infoWholeTarget.setUpdateBy(SecurityUtils.getLoginUser().getUser().getDept().getDeptName() + "-" + SecurityUtils.getLoginUser().getUser().getNickName());
        //上报的时候判断任务信息 或者 指标信息是否为空
        if (infoWholeTarget.getAuditStatus() != null) {
            if (infoWholeTarget.getAuditStatus() == 1) {
                InfoWholePlan infoWholePlan = new InfoWholePlan();
                infoWholePlan.setWholeId(infoWholeTarget.getId());
                List<InfoWholePlan> infoWholePlans = infoWholePlanMapper.selectInfoWholePlansId(infoWholePlan.getWholeId());

                InfoWholeIndex infoWholeIndex = new InfoWholeIndex();
                infoWholeIndex.setWholeId(infoWholeTarget.getId());
                List<InfoWholeIndex> infoWholeIndices = infoWholeIndexMapper.selectInfoWholeIndicesId(infoWholeIndex.getWholeId());
                if (infoWholePlans.isEmpty()) {
                    throw new RuntimeException("任务信息为空");
                }
                if (infoWholeIndices.isEmpty()) {
                    throw new RuntimeException("指标信息为空");
                }
            }

            //1.修改项目状态
            infoWholeTargetMapper.updateInfoWholeTarget(infoWholeTarget);
            //如果审核通过往评价里插数据
            if(infoWholeTarget.getAuditStatus()==3){
                InfoWholeEvaluate infoWholeEvaluate=new InfoWholeEvaluate();
                infoWholeEvaluate.setWholeId(infoWholeTarget.getId());
                InfoWholeTarget infoWholeTarget1=infoWholeTargetMapper.selectInfoWholeTargetsById(infoWholeTarget.getId());
                infoWholeEvaluate.setDeptId(infoWholeTarget1.getDepartmentId());
                infoWholeEvaluate.setDepartmentId(infoWholeTarget1.getDeptId());
                infoWholeEvaluate.setAuditOpinion(infoWholeTarget1.getAuditOpinion());
                String ridn=infoWholeTarget1.getIndustryTitle();
                SysDept sysDept=sysDeptMapper.selectDeptListsss(ridn);
                infoWholeEvaluate.setReleaseDepartmentId(sysDept.getDeptId());
                infoWholeEvaluate.setOverallGoals(infoWholeTarget1.getOverallGoals());
                infoWholeEvaluate.setDeptName(infoWholeTarget1.getDepartmentName());
                SysDept sysDept1=sysDeptMapper.selectDeptById(infoWholeTarget1.getDeptId());
                infoWholeEvaluate.setDepartmentName(sysDept1.getDeptName());
                infoWholeEvaluate.setYsdwName(infoWholeTarget1.getIndustryTitle());
                infoWholeEvaluateMapper.insertInfoWholeEvaluate(infoWholeEvaluate);
            }
        }
            return 1;
    }



    /**
     * 批量删除目标管理-部门整体信息
     *
     * @param ids 需要删除的目标管理-部门整体信息主键
     * @return 结果
     */
    @Override
    public int deleteInfoWholeTargetByIds(Long[] ids) {
        return infoWholeTargetMapper.deleteInfoWholeTargetByIds(ids);
    }

    /**
     * 删除目标管理-部门整体信息信息
     *
     * @param id 目标管理-部门整体信息主键
     * @return 结果
     */
    @Override
    public int deleteInfoWholeTargetById(Long id) {
        return infoWholeTargetMapper.deleteInfoWholeTargetById(id);
    }

    //查询汇总信息
    @Override
    public List<InfoWholeSummarizingVo> selectInfoWholeSummarizingVoList(String industryTitle) {
        List<InfoWholeSummarizingVo> list = infoWholeTargetMapper.selectInfoWholeSummarizingVoList(industryTitle);
        return list;
    }

    @Override
    public List<InfoWholeTarget> auditInfoWholeTargetList(InfoWholeTarget infoWholeTarget) {
        return infoWholeTargetMapper.selectAuditInfoWholeTargetList(infoWholeTarget);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int batchInsert(InfoBulkAddVo infoBulkAddVos) {
        try {
            List<InfoWholeTarget> infoWholeTargets = infoBulkAddVos.getInfoWholeTargets();
            List<InfoWholeIndex> infoWholeIndices = infoBulkAddVos.getInfoWholeIndices();
            List<InfoWholePlan> infoWholePlans = infoBulkAddVos.getInfoWholePlans();
            for (InfoWholeTarget infoWholeTarget : infoWholeTargets) {
                infoWholeTargetMapper.insertInfoWholeTarget(infoWholeTarget);
                for (InfoWholeIndex infoWholeIndex : infoWholeIndices) {
                    infoWholeIndex.setWholeId(infoWholeTarget.getId());
                    infoWholeIndexMapper.insertInfoWholeIndex(infoWholeIndex);
                }
                for (InfoWholePlan infoWholePlan : infoWholePlans) {
                    infoWholePlan.setWholeId(infoWholeTarget.getId());
                    infoWholePlanMapper.insertInfoWholePlan(infoWholePlan);
                }
            }

            return 1;
        } catch (Exception e) {
            System.out.println(e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
    }

    @Override
    public SysDept selectDeptById(Long deptId) {
        return infoWholeTargetMapper.selectDeptById(deptId);
    }

    @Override
    public List<InfoWholeTarget> selectProjectByDeptId(Long deptId) {
        return infoWholeTargetMapper.selectProjectByDeptId(deptId);
    }

    @Override
    public InfoWholeTarget selectInfoWholeTargetsById(Long wholeId) {
        return infoWholeTargetMapper.selectInfoWholeTargetsById(wholeId);
    }

    @Override
    public List<InfoWholeTarget> selectInfoWholeTargetLists(InfoWholeTarget infoWholeTarget) {
        //当前登录人
        List<SysRole> sysRoles=SecurityUtils.getLoginUser().getUser().getRoles();
        //拥有审计处角色的查询所有信息
        List<SysRole> roles=sysRoles.stream().filter(r->r.getRoleKey().equals("sjc")).collect(Collectors.toList());
        if(roles.size()>0){
            return infoWholeTargetMapper.selectInfoWholeTargetLists(null);
        }
        return infoWholeTargetMapper.selectInfoWholeTargetLists(SecurityUtils.getLoginUser().getUser().getDeptId());
    }


}
